<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
<title>在线投票系统</title>
<link href="main.css" rel="stylesheet" type="text/css">
<script src="js/jquery-2.1.1.min.js"></script>
<script>

var as = ["A","B","C","D","E","F"];
    function changeColor(id){
    	for(var i = 0 ; i < as.length;i++){
    		var av = as[i];
    		if(av == id){
    			$("#"+id).addClass("t_on");
    			$("#answer").val(id);
    		}else{
    			$("#"+av).removeClass("t_on");
    		}    		
    	}
   }   

 function chageTextValue(number){
	if ( number == 'DELETE' ) {
		var value = $("#name").text();
		value = value.substring(0, value.length - 1  );
		$("#name").text( value );
	}else if ( number == '.' ) {
		var value = $("#name").text();
		var index = value.indexOf('.');
		if ( index == -1 ) {
			value = value + number ;
			$("#name").text( value );
		}
	}else {
		var value = $("#name").text();
		value = value + number ;
		$("#name").text( value );
	}
	
 }


function commitInfo(){

	var value = $("#name").text();
	
	$.ajax( {
		url : "vote?answer=" + value ,
		type : 'post' ,
		datatype : 'json' , 
		success : function( data ){
			if ( data == '0000' ) {
				isSuccess = 1 ;
				$("#error").text("投票成功");
				$("#error").css({"color":"red"});
				//alert("提交成功" + data);
			} else if(data == '0001') {
				$("#error").text("投票失败");
				$("#error").css({"color":"red"});
				//alert("提交失败" + data);
			}else if(data == '0002') {
				$("#error").text("投票失败");
				$("#error").css({"color":"red"});
				//alert("问题获取失败" + data);
			}else if(data =="0003"){
				$("#error").text("投票还未开始");
				$("#error").css({"color":"red"}); 
			}else if(data =="0004"){
				$("#error").text("已经投票");
				$("#error").css({"color":"red"});
			} else if ( data =="0005" ) {
				$("#error").text("请参考规则");
				$("#error").css({"color":"red"});
			} else if ( data =="0005" ) {
				$("#error").text("您没有登录");
				$("#error").css({"color":"red"});
			}
							
		} ,
		error : function(){
			alert( '加载失败' );
		}
		
	});
		
	setTimeout( "clearERROR();" , 3000 );
	
}
 
 
</script>
<body>
	<div class="wrap">
		<div class="logo clearfix">
			<div class="logo_l">
			</div>
			<div class="logo_r">
			</div>
		</div>
		<center>
			<span style="font-size:30px;font-weight: bold;" >在线投票系统</span>
		</center>
		<div class="main_inp win50">
			<ul>
				<li>
					<span class="inp_txt" name="name" id="name" style="color: black;"></span>
				</li>
			</ul>
		</div>
		<center>
			<span id="error" style="font-size:20px;color: red;" ></span>
		</center>
		<div class="main_answer_btn win70 clearfix">
			<ul>
				<li><a href="javascript:;" onclick="chageTextValue('1');" id="bt_1">1</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('2');" id="bt_2">2</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('3');" id="bt_3">3</a></li>
				<!--
				<li><a href="javascript:;" onclick="chageTextValue('4');" id="bt_4">4</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('5');" id="bt_5">5</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('6');" id="bt_6">6</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('7');" id="bt_7">7</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('8');" id="bt_8">8</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('9');" id="bt_9">9</a></li>
				-->
				<li><a href="javascript:;" onclick="commitInfo();" id="SURE">确定</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('.');" id="bt_D">.</a></li>
				<li><a href="javascript:;" onclick="chageTextValue('DELETE');" id="DELETE">删除</a></li>
			</ul>
		</div>
		<div class="mian_txt mt50" style="font-size:12px" >
			<p>使用方法:</p>
			<p>1.点击投票按钮,点击确认即可</p>
			<p>2.如需修改,点击删除键,重新输入即可</p>
		</div>
	</div>
	
<script>

function clearERROR(){
	$("#error").text("");
	$("#name").text("");
}

$("#loginsubmit").click(function(){

	var ans = $("#answer").val();
	if(!ans){
		//alert("请选择答案");
		return;
	}
	
	$.ajax( {
			url : "vote?answer=" + $("#answer").val(),
			type : 'post' ,
			datatype : 'json' , 
			success : function( data ){
				if ( data == '0000' ) {
					isSuccess = 1 ;
					$("#error").text("投票成功");
					$("#error").css({"color":"red"});
					//alert("提交成功" + data);
				} else if(data == '0001') {
					$("#error").text("投票失败");
					$("#error").css({"color":"red"});
					//alert("提交失败" + data);
				}else if(data == '0002') {
					$("#error").text("投票失败");
					$("#error").css({"color":"red"});
					//alert("问题获取失败" + data);
				}else if(data =="0003"){
					$("#error").text("投票还未开始");
					$("#error").css({"color":"red"}); 
				}else if(data =="0004"){
					$("#error").text("已经投票");
					$("#error").css({"color":"red"});
				}
								
			} ,
			error : function(){
				alert( '加载失败' );
			}
			
	});
	
	
});

</script>

</body>
</html>
